package com.sh.suhe.utildemos.book.Presenter;

import android.content.Context;
import android.util.Log;

import com.sh.suhe.utildemos.book.base.BasePresenter;
import com.sh.suhe.utildemos.book.base.LoadCallBack;
import com.sh.suhe.utildemos.utils.JsonUtil;
import com.xmread.cn.Ui.entity.BookShelfBean;
import com.xmread.cn.bean.CommonResult;
import com.xmread.cn.config.AppConfig;
import com.xmread.cn.network.BookService;
import com.xmread.cn.network.ParamsUtil;
import com.xmread.cn.network.ResponceSubscriber;
import com.xmread.cn.network.RetrofitManager;
import com.xmread.cn.network.RxSchedulerUtils;

import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class BookShelfPresenter extends BasePresenter {
    private Context context;

    public void setCache(int i) {
    }

    public BookShelfPresenter(LoadCallBack loadCallBack, Context context2) {
        this.view = loadCallBack;
        this.context = context2;
    }

    public LoadCallBack getView() {
        return this.view;
    }

    public void getData() {
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(AppConfig.baseUrl)
//                .addConverterFactory(ScalarsConverterFactory.create()) //设置 Json 转换器
//                .addCallAdapterFactory(RxJava3CallAdapterFactory.create()).build(); //RxJava3 适配器
//        BookService httpService = retrofit.create(BookService.class);
//
//        httpService.getBookShalf(ParamsUtil.setSignParams(ParamsUtil.getDefaltParams()))
//                .subscribeOn(Schedulers.io()) //rxjava3的Schedulers
//                .observeOn(AndroidSchedulers.mainThread())  // rxjava3的AndroidSchedulers
//                .subscribe(new Observer<ResponseBody>() {  // rxjava3.core.Observer
//                    @Override
//                    public void onSubscribe(@NonNull Disposable d) {
//                        Log.e("BookShelfPresenter", "");
//                    }
//
//                    @Override
//                    public void onNext(@NonNull ResponseBody s) {
//
//                    Log.e("BookShelfPresenter",JsonUtil.object2Json(s));
//                    }
//
//                    @Override
//                    public void onError(@NonNull Throwable e) {
//                        Log.e("BookShelfPresenter", e.getLocalizedMessage());
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });

       RetrofitManager.getDefault().getBookShalf(ParamsUtil.setSignParams(ParamsUtil.getDefaltParams()))
                .compose(RxSchedulerUtils.normalSchedulersTransformer())
                .subscribe(new Observer<CommonResult<BookShelfBean>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("BookShelfPresenter", "");
                    }

                    @Override
                    public void onNext(@NonNull CommonResult<BookShelfBean> o) {
                        Log.e("BookShelfPresenter", JsonUtil.object2Json(o));
                        view.onLoad(o);

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("BookShelfPresenter", e.getLocalizedMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}
